<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router';
import { useAllDataStore } from '@/stores'

const store = useAllDataStore()
const tags = computed(() => store.state.tags)
const route = useRoute()
const router = useRouter()

const handleMenu = (tag) => {
    router.push({
        path: tag.path,
        meta: { keepAlive: true }
    })
    store.selectMenu(tag)
}

const handleClose = (tag, index) => {
    //通过pinia管理的
    store.updateTags(tag)
    // 如果点击的关闭的tag 不是对应的当前页面
    if (tag.name !== route.name) return
    if (index === store.state.tags.length) {
        store.selectMenu(tags.value[index - 1])
        router.push({
            path: tags.value[index - 1].path,
            meta: { keepAlive: true }
        })
    } else {
        store.selectMenu(tags.value[index])
        router.push({
            path: tags.value[index].path,
            meta: { keepAlive: true }
        })
    }
}
</script>

<template>
    <div class="tags-container">
        <div class="tags-wrapper">
            <div
                v-for="(tag, index) in tags"
                :key="tag.name"
                :class="['custom-tag', { 'active': route.path === tag.path }]"
                @click="handleMenu(tag)"
            >
                <span class="tag-content">
                    <el-icon v-if="tag.name === 'home'" class="home-icon"><House /></el-icon>
                    {{ tag.label }}
                </span>
                <el-icon 
                    v-if="tag.name !== 'home'" 
                    class="close-icon"
                    @click.stop="handleClose(tag, index)"
                >
                    <Close />
                </el-icon>
            </div>
        </div>
    </div>
</template>

<style lang="less" scoped>
.tags-container {
    padding: 8px 16px;
    background-color: #f8f9fa;
    border-bottom: 1px solid #e9ecef;
}

.tags-wrapper {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 4px 0;
    scrollbar-width: none;
    &::-webkit-scrollbar {
        display: none;
    }
}

.custom-tag {
    display: inline-flex;
    align-items: center;
    padding: 6px 12px;
    background-color: #ffffff;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    border: 1px solid #e9ecef;
    color: #495057;
    font-size: 13px;
    font-weight: 500;
    user-select: none;

    &:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        border-color: #dee2e6;
    }

    &.active {
        background-color: #228be6;
        color: #ffffff;
        border-color: #228be6;
        box-shadow: 0 2px 4px rgba(34, 139, 230, 0.2);
        font-weight: 600;

        .close-icon {
            color: #ffffff;
            &:hover {
                background-color: rgba(255, 255, 255, 0.2);
            }
        }
    }

    .tag-content {
        display: flex;
        align-items: center;
        gap: 6px;
        margin-right: 6px;

        .home-icon {
            font-size: 14px;
        }
    }

    .close-icon {
        font-size: 12px;
        color: #adb5bd;
        padding: 2px;
        border-radius: 4px;
        transition: all 0.2s;

        &:hover {
            background-color: #e9ecef;
            color: #495057;
        }
    }
}
</style>